package com.itheima.tanhua.utils;

import cn.hutool.core.convert.Convert;
import com.baidu.aip.face.AipFace;
import org.json.JSONObject;

import java.util.HashMap;

public class FaceUtil {

    public static final String APP_ID = "26037091";
    public static final String API_KEY = "cFk6x2fCVn57ySrcwCRsBo78";
    public static final String SECRET_KEY = "Sz0uza2eGhj2ZU971mBfHQHo1LIthMsA";

    private FaceUtil(){}

    //提供一个监测方法：输入参数imagePath  返回值 err_code  0：是人  非0：非人

    public static Integer checkFace(String imagePath){
        // 初始化一个AipFace
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        // 调用接口
        //String image = "取决于image_type参数，传入BASE64字符串或URL字符串或FACE_TOKEN字符串";
        String imageType = "URL";
        // 人脸检测
        JSONObject res = client.detect(imagePath, imageType, options);
        return Convert.toInt(res.get("error_code"));
    }
}
